package com.pengheng.sink;

import com.pengheng.bean.WaterSensor;
import com.pengheng.sink.common.CommonJdbcSink;
import com.pengheng.sink.common.WaterSensorSupplier;
import com.pengheng.transformation.aggregation.WaterSensorMapFunction;
import org.apache.flink.api.common.restartstrategy.RestartStrategies;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.sink.SinkFunction;

import java.io.Serializable;

public class SinkMySQL implements Serializable {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        env.setRestartStrategy(RestartStrategies.fixedDelayRestart(3, 5000));

        SingleOutputStreamOperator<WaterSensor> sensorDS = env.socketTextStream("192.168.10.100", 7777)
                .map(new WaterSensorMapFunction());


        String sql = "insert into ws values(?,?,?)";
        CommonJdbcSink<WaterSensor> waterSensorCommonJdbcSink = new CommonJdbcSink<>();
        WaterSensorSupplier waterSensorSupplier = new WaterSensorSupplier();
        SinkFunction<WaterSensor> sinkFunction = waterSensorCommonJdbcSink.extracted(sql, waterSensorSupplier);
        sensorDS.addSink(sinkFunction);
        env.execute();
    }
}
